<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/15
 * Time: 9:51
 */

class BatchOrder extends MY_Controller{
    function __construct() {
        parent::__construct();
        //加载模型
        $this->load->model('batchorder_model');
    }

    /**
     * 公共首页
     */
    public function index(){
        $this->GetBatchOrderList();
    }

    /**
     * 获取批量订单列表
     */
    public function GetBatchOrderList(){
        $gp=gp();
        //条件判断
        $gp['page']?$gp['page']=$gp['page']:$gp['page']=1;
        $str='?keyword='.$gp['keyword'];
        //分页查询
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $config['base_url'] = blang_url('orders/BatchOrder/GetBatchOrderList'.$str);
        $config['total_rows'] = $this->batchorder_model->CountBatchOrderTotal($gp['keyword']);
        $config['uri_segment'] = config_item('enable_lang')?5:4;
        $config['per_page'] = 15;
        $this->pagination->initialize($config);
        $data['pageShow']= $this->pagination->create_links();
        //数据
        $BatchOrderList=$this->batchorder_model->GetBatchOrderList($gp['keyword'],$config['per_page'],($gp['page']-1)*$config['per_page']);
        $data['BatchOrderList']=$this->RefactorBatchOrderData($BatchOrderList);//重构数据
        //客户数据
        $data['clineList']=$this->batchorder_model->GetClineList();
        //查询条件
        $data['str']=$str;
        $this->load->view('defaults/BatchOrderList',$data);
        $this->load->view('defaults/common/footer',$data);
    }

    /**
     * 导出批量单数据
     */
    public function ExportBatchOrderData(){
        $gp=gp();
        $BatchOrderList=$this->batchorder_model->GetBatchOrderList($gp['keyword']);
        $newData=$this->RefactorBatchOrderData($BatchOrderList);//重构数据
        $dataList=array(
            'site'=>'站点',
            'orderId'=>'订单号',
            'sku'=>'产品SKU/数量',
            'name'=>'客户',
            'handworkCostTotal'=>'手工费',
            'otherCostTotal'=>'其他费用',
            'accessoriesCostTotal'=>'配件费用',
            'freight'=>'运费',
            'total'=>'总计',
            'currency'=>'币种',
            'time'=>'建单日期',
            'is_charge_name'=>'是否已经扣款'
        );
        excel_output('BatchOrder',$dataList,$newData);
    }
    /**
     * 添加批量单
     */
    public function AddBatchOrder(){
        $gp=gp();
        //重构数据
        $gp=$this->RefactorBatchOrderNewData($gp);
        //下单时间
        $gp['time']=date('Y-m-d H:i:s',time());
        //录入人
        $gp['recordName']=$this->users['name'];
        //上传聊天附件
        if($_FILES['chatRecords']['name']) {
            $uploaddir = './resource/uploadfiles/file/uploadify/';//上传的文件保存在当前目录
            $gp['chatRecords'] = uploadfile($_FILES['chatRecords'], $uploaddir);
        }
        //对数据入库
        $this->batchorder_model->AddBatchOrder($gp);
        //显示列表
        redirect(blang_url('orders/BatchOrder'));
    }

    /**
     * 更新批量单
     */
    public function UpdateBatchOrder(){
        $gp=gp();
        //重构数据
        $gp=$this->RefactorBatchOrderNewData($gp);
        //上传聊天附件
        if($_FILES['chatRecords']['name']) {
            $uploaddir = './resource/uploadfiles/file/uploadify/';//上传的文件保存在当前目录
            $gp['chatRecords'] = uploadfile($_FILES['chatRecords'], $uploaddir);
        }else{
            unset($gp['chatRecords']);
        }
        $this->batchorder_model->UpdateBatchOrder($gp['orderId'],$gp);
        //显示列表
        $this->GetBatchOrderList();
        redirect(blang_url('orders/BatchOrder'));
    }

    /**
     * 重构批量单数据
     * @param $BatchOrderData
     */
    public function RefactorBatchOrderData($BatchOrderData){
        foreach($BatchOrderData as &$row){
            //计算手工费
            $row['is_charge_name']=$row['is_charge']?'已扣':'未扣';
            if($row['handworkCost']) {
                $handworkCost = json_decode($row['handworkCost']);
                $handworkCostTotal = 0;
                foreach ($handworkCost as $handwork) {
                    $handworkCostTotal += $handwork[1] * $handwork[2];
                }
                $row['handworkCostTotal'] = $handworkCostTotal;
            }else{
                $row['handworkCostTotal'] = 0;
            }
            //计算其他费用
            if($row['otherCost']) {
                $otherCost = json_decode($row['otherCost']);
                $otherCostTotal = 0;
                foreach ($otherCost as $other) {
                    $otherCostTotal += $other[1] * $other[2];
                }
                $row['otherCostTotal'] = $otherCostTotal;
            }else{
                $row['otherCostTotal'] = 0;
            }
            //计算配件费
            if($row['accessoriesCost']) {
                $accessoriesCost = json_decode($row['accessoriesCost']);
                $accessoriesCostTotal = 0;
                foreach ($accessoriesCost as $accessories) {
                    $accessoriesCostTotal += $accessories[1] * $accessories[2];
                }
                $row['accessoriesCostTotal'] = $accessoriesCostTotal;
            }else{
                $row['accessoriesCostTotal'] = 0;
            }
        }
        unset($row);
        return $BatchOrderData;
    }

    /**
     * 重构批量单新数据
     * @param $gp
     */
    public function RefactorBatchOrderNewData($gp){
        //产品SKU数组
        $skuArray=array();
        //手工费数组
        $handworkArray=array();
        //其他费用数组
        $otherArray=array();
        //配件费数组
        $accessoriesArray=array();
        //重构数据
        foreach($gp['repairItemsText'] as $key=>$val){
            if($gp['repairItemsText'][$key]) {
                $handworkArray[] = array($gp['repairItemsText'][$key], $gp['repairItemsNum'][$key], $gp['repairItemsCost'][$key]);
            }
        }
        foreach($gp['otherText'] as $key=>$val){
            if($gp['otherText'][$key]) {
                $otherArray[] = array($gp['otherText'][$key], $gp['otherNum'][$key], $gp['otherCost'][$key]);
            }
        }
        foreach($gp['accessoriesText'] as $key=>$val){
            if($gp['accessoriesText'][$key]) {
                $accessoriesArray[] = array($gp['accessoriesText'][$key], $gp['accessoriesNum'][$key], $gp['accessoriesCost'][$key]);
            }
        }
        foreach($gp['skuText'] as $key=>$val){
            if($gp['skuText'][$key]) {
                $skuArray[] = array($gp['skuText'][$key], $gp['skuNum'][$key]);
            }
        }
        //删除没利用价值数据
        unset($gp['repairItemsText']);
        unset($gp['repairItemsNum']);
        unset($gp['repairItemsCost']);
        unset($gp['otherText']);
        unset($gp['otherNum']);
        unset($gp['otherCost']);
        unset($gp['accessoriesText']);
        unset($gp['accessoriesNum']);
        unset($gp['accessoriesCost']);
        unset($gp['skuText']);
        unset($gp['skuNum']);
        //数组转Json
        if($handworkArray) {
            $gp['handworkCost'] = json_encode($handworkArray);
        }else{
            $gp['handworkCost']='';
        }
        if($otherArray) {
            $gp['otherCost'] = json_encode($otherArray);
        }else{
            $gp['otherCost']='';
        }
        if($accessoriesArray) {
            $gp['accessoriesCost'] = json_encode($accessoriesArray);
        }else{
            $gp['accessoriesCost']='';
        }
        if($skuArray) {
            $gp['sku'] = json_encode($skuArray);
        }
        return $gp;
    }
}